package com.wavehi.exce.stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Author WaveHi
 * @Date 27/8/2021 上午 10:18
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Demonstrate the use of Stream
 */
public class StreamDemo1 {

    @Test
    public void testStreamUse1() {
        //获取测试数据
        int[] nums = {123, 2, 3, 34, 34, 34};
        List<int[]> ints = Arrays.asList(nums);
        //获取流
        Stream<int[]> stream4 = ints.stream();
        Stream<int[]> stream3 = ints.parallelStream();//并行流
        IntStream stream1 = Arrays.stream(nums);
        Stream<int[]> stream2 = Stream.of(nums);
        Stream.iterate(0,t -> t + 2).limit(10).forEach(x -> System.out.println(x));//无限流

        
    }

    @Test
    public void testStreamUse2() {
        //获取数据
        int[] nums = {123, 123, 123, 3, 43, 443, 545, 131, 453};

        //获取流
        IntStream stream = Arrays.stream(nums);

//        //对流进行筛选
//        stream.filter(s -> s > 20).forEach(System.out :: println);

        //对流中数据进行去重操作
//        System.out.println("After removing the duplicate data,the data in the stream is :");
//        stream.distinct().forEach(System.out :: println);

        //对流进行截断
//        System.out.println("The truncated data is :");
//        stream.limit(3).forEach(System.out :: println);

        //忽略流前面指定数量的数据
        System.out.println("After ignoring part of data,the data in the stream is :");
        stream.skip(5).forEach(System.out :: println);
    }

    @Test
    public void testStreamUse3() {
        //获取数据源
        String[] strs = {"Wow!", "Ok!", "Hello!", "Yes!", "No!"};

        //获取流
        Stream<String> stream = Arrays.stream(strs);

        //测试
//        double[] doubles = stream.mapToDouble(s -> s.length()).toArray();
//        System.out.println(Arrays.toString(doubles));
        Stream<String> stringStream = stream.flatMap(Stream::of);

    }

    @Test
    public void testStreamUse4() {
        //获取数据源
        String[] strs = {"Wow!","Hello!","Ok!","Yes!","NO!"};

        //获取流
        Stream<String> stream = Arrays.stream(strs);

        //判断流中的数据是否全部符合条件
//        System.out.print("Is the length of data in the stream greater than 5?");
//        System.out.println(stream.allMatch(s -> s.length() > 5));

        //判断流中的数据是否又符合条件的
//        System.out.print("Whether the length of data in the stream is greater than 5 ?");
//        System.out.println(stream.anyMatch(s -> s.length() > 5));

        //判断流中的数据是否都不满足条件
//        System.out.print("Whether the length of data in the stream is not greater than 5?:");
//        System.out.println(stream.noneMatch(s -> s.length() > 5));

        //统计流中的数据个数
        System.out.println("The count of data in the stream is :" + stream.count());

        //获取流中最大的数据
//        System.out.println("The max data int the stream is :" + stream.max((s1, s2) -> s1.compareTo(s2)));

        //获取流中最小的数据
//        System.out.println("The min data in the stream is :" + stream.min((s1, s2) -> s1.compareTo(s2)));

        //获取第一个数据
//        System.out.println("The first data in the stream is :" + stream.findFirst());

        //获取任意一个数据
        System.out.println("A data in the stream is :" + stream.findAny());
    }

    @Test
    public void testStreamUse5() {
        //获取数据源
        int[] nums = {32, 323, 343, 54, 345, 54};

        //获取流
        IntStream stream = Arrays.stream(nums);

        //对流数据进行归约
        System.out.println("The sum of data in the stream is :" + stream.reduce(0, (s1, s2) -> s1 + s2));

    }

}
